<?php

/**
 * visualization actions.
 *
 * @package    Redsea20
 * @subpackage visualization
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class visualizationActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $RID=$request->getParameter('RID');
      if (isset($RID))
      {
          $this->setLayout('emptylayout');
          $this->RID= $RID;
      }
      else
      {
        $this->setLayout('visualization');
      }
  }

  //Load Markers for Map
public function executeMap_Data(sfWebRequest $request)
  {
      $str="";
      $locationfilter=$request->getParameter('locationfilter');
      $tagsfilter=$request->getParameter('tagsfilter');
      $depthsfilter=$request->getParameter('depthsfilter');
      $unitsfilter=$request->getParameter('unitsfilter');
      $contributorsfilter=$request->getParameter('contributorsfilter');
      $startdatefilter=$request->getParameter('startdatefilter');
      $enddatefilter=$request->getParameter('enddatefilter');

      $filterQuery="";
      $ResearchIdfilterQuery="";
      $ResearchParameter=$request->getParameter('RID');

if (isset($locationfilter))
{
    $filterQuery=$filterQuery." and o.id in (".$locationfilter.")";
}
if (isset($tagsfilter))
{
    $filterQuery=$filterQuery." and odd.tag in (".$tagsfilter.")";
}
if (isset($contributorsfilter))
{
    $filterQuery=$filterQuery." and e.id in (".$contributorsfilter.")";
}
if (isset($depthsfilter))
{
    $filterQuery=$filterQuery." and od.observationdepth in (".$depthsfilter.")";
}
if (isset($unitsfilter))
{
    $filterQuery=$filterQuery." and u.id in (".$unitsfilter.")";
}
if (isset($startdatefilter) && isset($enddatefilter))
{
    $filterQuery=$filterQuery." and o.observationdate between '".$startdatefilter."' and '".$enddatefilter."'";
}

if (isset($ResearchParameter) && $ResearchParameter!="")
{
    $ResearchIdfilterQuery=$ResearchIdfilterQuery." and r.id =" .$ResearchParameter;
}





         $str=explode("flg_bio_and_nonbio_map_point", $this->getRequestParameter('search'));

         

          //$str=$this->getRequestParameter('search');
          if ($str[0]!=null && $str[0]!="")
          {
            $this->observationList = Doctrine_Query::create()
                                ->select('o.lat,o.lng,o.id')
                                ->from('observation o')
                                ->leftJoin('o.Observationdepth od')
                                ->leftJoin('od.Observationdepthdetail odd')
                                ->leftJoin('odd.Unit u')
                                ->leftJoin('o.Research r')
                                ->leftJoin('r.Entity e')
                                ->leftJoin('r.Privacy p')
                                ->where('odd.speciesid In ('.$str[0].')' . $ResearchIdfilterQuery .$filterQuery )
                                ->andWhere('odd.quantity>0 and p.privacytyperefid= '.Types::$RESEARCH_SCOPE[Types::$RESEARCH_SCOPE_PUBLIC])
                                ->execute();
          }

          if ($str[1]!=null && $str[1]!="")
          {
            $this->observationList_non = Doctrine_Query::create()
                                ->select('o.lat,o.lng,o.id')
                                ->from('observation o')
                                ->leftJoin('o.Observationdepth od')
                                ->leftJoin('od.Observationdepthattribute oda')
                                ->leftJoin('o.Research r')
                                ->leftJoin('r.Privacy p')
                                ->leftJoin('r.Entity e')
                                ->leftJoin('oda.Unit u')
                                ->where('oda.attributeid In ('.$str[1].') ' . $ResearchIdfilterQuery .$filterQuery )
                                ->andWhere('p.privacytyperefid= '.Types::$RESEARCH_SCOPE[Types::$RESEARCH_SCOPE_PUBLIC])
                                ->execute();
            
          }
          if(($str[0]==null || $str[0]=="")&&($str[1]==null || $str[1]==""))
          {
              $this->observationList = Doctrine_Query::create()
                                ->select('o.lat,o.lng,o.id')
                                ->from('observation o')
                                ->execute();
          }


        echo "<markers>";

        foreach($this->observationList as $r){
            echo "<marker ";
            $tag = "lat";
            echo ($tag. '="'.$r->getLat().'" ');
            $tag = "lng";
            echo ($tag. '="'.$r->getLng().'" ');
            $tag = "Location";
            echo ($tag. '="'.$r->getId().'" ');
            $tag = "str";
            echo ($tag. '="'.$str.'" ');
            $tag = "LocationName";
            echo ($tag. '="'.$r->getLocation().'" ');
            echo (' />');
        }
        foreach($this->observationList_non as $r){
            echo "<marker ";
            $tag = "lat";
            echo ($tag. '="'.$r->getLat().'" ');
            $tag = "lng";
            echo ($tag. '="'.$r->getLng().'" ');
            $tag = "Location";
            echo ($tag. '="'.$r->getId().'" ');
            $tag = "str";
            echo ($tag. '="'.$str.'" ');
            $tag = "LocationName";
            echo ($tag. '="'.$r->getLocation().'" ');
            echo (' />');
        }
        echo "</markers>";

        return sfView::NONE;
  }

  //// Load Overlay data
public function executeOverlay_Data(sfWebRequest $request)
  {
      $str="";
      if ($this->getRequestParameter('search')!=null && $this->getRequestParameter('search')!="")
      {

          $str=$this->getRequestParameter('search');
            $this->overlayData = Doctrine_Query::create()
                  ->from('researchoverlay ro')
		  ->where('ro.id In ('.$str.')')
                  ->execute();
      }

        echo "<markers>";

        foreach($this->overlayData as $r){
            echo "<marker ";
            $tag = "id";
            echo ($tag. '="'.$r->getId().'" ');
            $tag = "nlat";
            echo ($tag. '="'.$r->getNlat().'" ');
            $tag = "wlng";
            echo ($tag. '="'.$r->getWlng().'" ');
            $tag = "slat";
            echo ($tag. '="'.$r->getSlat().'" ');
            $tag = "elng";
            echo ($tag. '="'.$r->getElng().'" ');
            echo (' />');
        }
        echo "</markers>";

        return sfView::NONE;
  }
  //// Load Overlay Image
public function executeOverlay_Image(sfWebRequest $request)
  {
     $overlayid = 0;
      /**
       * IF PASSED PARAMETER IS NULL/0, THEN GET THE ID FROM SESSION
       */
      if($this->getRequestParameter('overlayid') != null && $this->getRequestParameter('overlayid') != '0')
      {
          $overlayid = $this->getRequestParameter('overlayid');
          $overlayList = Doctrine_Core::getTable('researchoverlay');
          $img = $overlayList->find($overlayid);
      //$entity = $entityList->find($this->getUser()->getAttribute('userid'));


          echo $img["overlayimage"];
      }


      return sfView::NONE;
  }
  /// Load Data for Pie Chart for Map Markers

  public function executePopuppie(sfWebRequest $request)
  {
      $params = "";// "375,374";
      $strtmp="";
    if ($this->getRequestParameter('search') !=null && $this->getRequestParameter('search') != "")
    { $andQuery=$request->getParameter('selectedSpecies');
    if (isset($andQuery) && $andQuery!="" )
        {
            $andQuery=" and odd.speciesid in (". $andQuery .")";
         }
         else
         {
             $andQuery="";
         }

            $this->chartDetail=Doctrine_Query::create()
                  ->select('odd.speciesid,sum(odd.quantity) as qty')
                  ->addSelect('s.species')
                  ->from('observationdepthdetail odd')
                  ->leftJoin('odd.Species s')
                  ->leftJoin('odd.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->where('o.id In ('.$this->getRequestParameter('search').')'. $andQuery)
                  ->andWhere('odd.quantity>0')
                  ->groupBy('speciesid')
                  ->orderBy('sum(odd.quantity) desc')
                  ->execute();


    }

    $output="";
    foreach($this->chartDetail as $item){
        $output= $output.$item->Species["species"].":".$item["qty"].":".$item->Observationdepth->Observation["location"]."|";



    }

    echo "BIOflgtype".$output;

    return sfView::NONE;
  }
  /// Auto Complete for Search
   public function executeAutocomplete(sfWebRequest $request)
  {



          $Species=Doctrine_Query::create()
                  ->select('s.id,s.Species')
                  ->from('species s')
                  ->orderby('s.species')
                  ->execute();
           $Attribute=Doctrine_Query::create()
                  ->select('a.id,a.attributename')
                  ->from('attribute a')
                  ->orderby('a.attributename')
                  ->execute();
            $Overlay=Doctrine_Query::create()
                  ->select('o.id,o.overlaytitle')
                  ->from('researchoverlay o')
                  ->leftJoin('o.Research r')
                  ->leftJoin('r.Privacy p')
                  ->where('p.privacytyperefid= '.Types::$RESEARCH_SCOPE[Types::$RESEARCH_SCOPE_PUBLIC])
                  ->orderby('o.overlaytitle')
                  ->execute();
          $str="";

                   foreach($Species as $r)
                   {
                    $str=$str.$r->getSpecies()."***Species###";
                    
                   }
                   foreach($Attribute as $r)
                   {
                    $str=$str.$r->getAttributename()."***Attributes###";
                   }
                   foreach($Overlay as $r)
                   {
                    $str=$str.$r->getOverlaytitle()."***Overlays###";
                   }

                    echo $str;
                  
		  return sfView::NONE;
  }
/// Load Citation for selected data
  public function executeSearchcitation(sfWebRequest $request)
  {

$RID=$request->getParameter('RID');



      $locationfilter=$request->getParameter('locationfilter');
      $tagsfilter=$request->getParameter('tagsfilter');
      $depthsfilter=$request->getParameter('depthsfilter');
      $unitsfilter=$request->getParameter('unitsfilter');
      $contributorsfilter=$request->getParameter('contributorsfilter');
      $startdatefilter=$request->getParameter('startdatefilter');
      $enddatefilter=$request->getParameter('enddatefilter');
      
      $filterQuery="";


if (isset($locationfilter))
{
    $filterQuery=$filterQuery." and o.id in (".$locationfilter.")";
}
if (isset($tagsfilter))
{
    $filterQuery=$filterQuery." and odd.tag in (".$tagsfilter.")";
}
if (isset($contributorsfilter))
{
    $filterQuery=$filterQuery." and e.id in (".$contributorsfilter.")";
}
if (isset($depthsfilter))
{
    $filterQuery=$filterQuery." and od.observationdepth in (".$depthsfilter.")";
}
if (isset($unitsfilter))
{
    $filterQuery=$filterQuery." and u.id in (".$unitsfilter.")";
}

if (isset($startdatefilter) && isset($enddatefilter))
{
    $filterQuery=$filterQuery." and o.observationdate between '".$startdatefilter."' and '".$enddatefilter."'";
}


if (isset($RID) && $RID!="")
{
    $RID=" and r.id=".$RID;
}
      $str= explode("flag_search_citation", $this->getRequestParameter('search'));


        if ($str[0] !=null && $str[0]!="")
            {
                  if ($str[1] =="BIO")
                  {
                  $this->flgCitation="BIO";
                  $this->Biocitation=Doctrine_Query::create()
                  ->select('r.citation,odd.id,o.id,od.id')
                  ->from('observationdepthdetail odd')
                  ->leftJoin('odd.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('o.Research r')
                  ->leftJoin('odd.Species s')
                  ->leftJoin('r.Entity e')
                  ->leftJoin('odd.Unit u')
                  ->where('s.id in ('.$str[0].') '. $RID. $filterQuery)
                  ->groupBy('r.citation')
                  ->execute();
                  }
                  else if ($str[1] =="NONBIO")
                  {
                  $this->flgCitation="NONBIO";
                  $this->NONBiocitation=Doctrine_Query::create()
                  ->select('r.citation,oda.id,o.id,od.id')
                  ->from('observationdepthattribute oda')
                  ->leftJoin('oda.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('o.Research r')
                  ->leftJoin('oda.Attribute a')
                  ->leftJoin('r.Entity e')
                  ->leftJoin('oda.Unit u')
                  ->where('a.id in ('.$str[0].') '. $RID. $filterQuery)
                  ->groupBy('r.citation')
                  ->execute();
                  }

                  else if ($str[1] =="OVERLAY")
                  {
                  $this->flgCitation="OVERLAY";
                  $this->Overlaycitation=Doctrine_Query::create()
                  ->select('ro.id,r.citation')
                  ->from('researchoverlay ro')
		  ->leftJoin('ro.Research r')
                  ->where('ro.id in ('.$str[0].') ' .$RID)
                  ->groupBy('r.citation')
                  ->execute();
                  }

            }


  }
///// Search Results

  public function executeSearch(sfWebRequest $request)
{

    $para=$request->getParameter('RID');

     $str="";
     $this->pgTag="";
     if(isset($para))
     {
         $str= explode("flag_search_d_pg_t", $para);

                if ($str[1]!=null && ($str[1]=="BIO" || $str[1]=="ALL"))
                {

                  $this->pgTag=$str[1];
                  $this->biodata = new sfDoctrinePager('', '100');
                  $q = Doctrine_Query::create()
                  ->select('odd.speciesid,s.species as spname')
                  ->from('observationdepthdetail odd')
                  ->leftJoin('odd.Species s')
		  ->leftJoin('odd.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('o.Research r')
                  ->leftJoin('r.Entity rr')
                  ->where('r.id='.$str[0])
                  ->andWhere('odd.quantity>0')
                  ->groupBy('odd.speciesid');

                  $this->biodata->setQuery($q);
                  if ($str[2]!=null && $str[2]!="")
                    {
                        $this->biodata->setPage($str[2]);
                        $this->biodata->init();
                    }
                  else {
                        $this->biodata->setPage('1');
                        $this->biodata->init();
                    }

                }
                 if ($str[1]!=null && ($str[1]=="NONBIO" || $str[1]=="ALL"))
                {

                  $this->pgTag=$str[1];
                  $this->nonbiodata = new sfDoctrinePager('', '100');
                  $q = Doctrine_Query::create()
//                
                  ->select('a.attributename,a.id,ada.id')
		  ->from('observationdepthattribute ada')
                  ->leftJoin('ada.Attribute a')
                  ->leftJoin('ada.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('o.Research r')
                  ->leftJoin('r.Entity rr')
                  ->where('r.id='.$str[0])
                  ->andWhere('ada.attributevalue>0')
                  ->groupBy('a.attributename');


                  $this->nonbiodata->setQuery($q);
                  if ($str[2]!=null && $str[2]!="")
                    {
                        $this->nonbiodata->setPage($str[2]);
                        $this->nonbiodata->init();
                    }
                  else {
                        $this->nonbiodata->setPage('1');
                        $this->nonbiodata->init();
                    }
                }
                if ($str[1]!=null && ($str[1]=="OVERLAY" || $str[1]=="ALL"))
                {

                  $this->pgTag=$str[1];
                  $this->overlaydata = new sfDoctrinePager('', '100');
                  $q = Doctrine_Query::create()
                  ->select('ro.id,ro.overlaytitle')
                  ->from('researchoverlay ro')
		  ->leftJoin('ro.Research r')
                  ->leftJoin('r.Entity rr')
                   ->where('r.id='.$str[0]);
                  

                  $this->overlaydata->setQuery($q);
                  if ($str[2]!=null && $str[2]!="")
                    {
                        $this->overlaydata->setPage($str[2]);
                        $this->overlaydata->init();
                    }
                  else {
                        $this->overlaydata->setPage('1');
                        $this->overlaydata->init();
                    }
                }
     }
     else
     {
      if ($this->getRequestParameter('search')!=null && $this->getRequestParameter('search')!="")
      {
              $str= explode("flag_search_d_pg_t", $this->getRequestParameter('search'));

                if ($str[1]!=null && ($str[1]=="BIO" || $str[1]=="ALL"))
                {

                  $this->pgTag=$str[1];
                  $this->biodata = new sfDoctrinePager('', '100');
                  $q = Doctrine_Query::create()
                  ->select('odd.speciesid,s.species as spname')
                  ->from('observationdepthdetail odd')
                  ->leftJoin('odd.Species s')
		  ->leftJoin('odd.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('o.Research r')
                  ->leftJoin('r.Privacy p')
                  ->leftJoin('r.Entity rr')
                  ->where('odd.quantity>0 and p.privacytyperefid= '.Types::$RESEARCH_SCOPE[Types::$RESEARCH_SCOPE_PUBLIC])
                  ->andWhere("odd.tag like '%".$str[0]."%' or r.title like '%".$str[0]."%' or o.location like '%".$str[0]."%' or s.species like '%".$str[0]."%' or rr.firstname like '%".$str[0]."%' or rr.lastname like '%".$str[0]."%' or substr((cast(o.observationdate as char(800))),1,10) like '%".$str[0]."%'")
                  ->groupBy('odd.speciesid');

                  $this->biodata->setQuery($q);
                  if ($str[2]!=null && $str[2]!="")
                    {
                        $this->biodata->setPage($str[2]);
                        $this->biodata->init();
                    }
                  else {
                        $this->biodata->setPage('1');
                        $this->biodata->init();
                    }

                }
                 if ($str[1]!=null && ($str[1]=="NONBIO" || $str[1]=="ALL"))
                {

                  $this->pgTag=$str[1];
                  $this->nonbiodata = new sfDoctrinePager('', '100');
                  $q = Doctrine_Query::create()
                  ->select('a.attributename,a.id,ada.id')
		  ->from('observationdepthattribute ada')
                  ->leftJoin('ada.Attribute a')
                  ->leftJoin('ada.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('o.Research r')
                  ->leftJoin('r.Privacy p')
                  ->leftJoin('r.Entity rr')
                  ->where("ada.attributevalue > 0  and p.privacytyperefid= ".Types::$RESEARCH_SCOPE[Types::$RESEARCH_SCOPE_PUBLIC])
                  ->andWhere("(a.attributename like '%".$str[0]."%' OR rr.Firstname like '%".$str[0]."%' OR rr.Lastname like '%".$str[0]."%' OR r.title like '%".$str[0]."%' OR o.location like '%".$str[0]."%' OR substr((cast(o.observationdate as char(800))),1,10) like '%".$str[0]."%')")
                  ->groupBy('a.attributename');


                  $this->nonbiodata->setQuery($q);
                  if ($str[2]!=null && $str[2]!="")
                    {
                        $this->nonbiodata->setPage($str[2]);
                        $this->nonbiodata->init();
                    }
                  else {
                        $this->nonbiodata->setPage('1');
                        $this->nonbiodata->init();
                    }
                }
                if ($str[1]!=null && ($str[1]=="OVERLAY" || $str[1]=="ALL"))
                {

                  $this->pgTag=$str[1];
                  $this->overlaydata = new sfDoctrinePager('', '100');
                  $q = Doctrine_Query::create()
                  ->select('ro.id,ro.overlaytitle')
                  ->from('researchoverlay ro')
		  ->leftJoin('ro.Research r')
                  ->leftJoin('r.Privacy p')
                  ->leftJoin('r.Entity rr')
                  ->where('p.privacytyperefid= '.Types::$RESEARCH_SCOPE[Types::$RESEARCH_SCOPE_PUBLIC])
                  ->andWhere("(r.title like '%".$str[0]."%' or ro.overlaytitle like '%".$str[0]."%' or rr.Firstname like '%".$str[0]."%' or rr.Lastname like '%".$str[0]."%')");

                  $this->overlaydata->setQuery($q);
                  if ($str[2]!=null && $str[2]!="")
                    {
                        $this->overlaydata->setPage($str[2]);
                        $this->overlaydata->init();
                    }
                  else {
                        $this->overlaydata->setPage('1');
                        $this->overlaydata->init();
                    }
                }
    }
     }

    }



    ////////// Species Graph Data

public function executeBiographdata(sfWebRequest $request)
  {
      $params = "";// "375,374";
      $strtmp="";
    if ($this->getRequestParameter('search') !=null && $this->getRequestParameter('search') != "")
    {



                  $this->chartDetail=Doctrine_Query::create()
                  ->select('odd.id,od.id,o.location,odd.quantity,s.species,o.lat,o.lng,o.observationdate,o.remarks')
                  ->from('observationdepthdetail odd')
		  ->leftJoin('odd.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('odd.Species s')
                  ->where('o.id In ('.$this->getRequestParameter('search').')')
                  ->andWhere('s.id In ('.$this->getRequestParameter('Selectedspecies').')')
                  ->andWhere('odd.quantity>0')
                  ->orderBy('s.id')
                  ->execute();



    }

    $output="";
    foreach($this->chartDetail as $item){
        $time = strtotime($item->Observationdepth->Observation["observationdate"]);
        $year=date('Y', $time);


        $output=$output.$item->Observationdepth->Observation["id"].":".$item->Species["species"].":".$item->Observationdepth->Observation["location"].":".$item["quantity"].":".$item->Observationdepth->Observation["lat"].":".$item->Observationdepth->Observation["lng"].":".$year.":".$item->Observationdepth->Observation["remarks"].":".$item->Observationdepth->Observation["observationdate"]."|";



    }
    echo $output;

    return sfView::NONE;
  }
// Nonbio Graph data
  public function executeNonbiographdata(sfWebRequest $request)
  {
      $params = "";// "375,374";
      $strtmp="";
    if ($this->getRequestParameter('search') !=null && $this->getRequestParameter('search') != "")
    {
                  $this->chartDetail=Doctrine_Query::create()
                  ->select('odd.id,od.id,o.location,odd.attributevalue,s.attributename,o.lat,o.lng,o.observationdate,o.remarks,od.observationdepth')
                  ->from('observationdepthattribute odd')
		  ->leftJoin('odd.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('odd.Attribute s')
                  ->where('o.id In ('.$this->getRequestParameter('search').')')
                  ->andWhere('s.id In ('.$this->getRequestParameter('Selectedattribute').')')
                  ->andWhere('odd.attributevalue>0')
                  ->orderBy('o.location')
                  ->orderBy('od.observationdepth')
                  ->execute();
    }

    $output="";
    foreach($this->chartDetail as $item){
        $time = strtotime($item->Observationdepth->Observation["observationdate"]);
        $year=date('Y', $time);


        $output=$output.$item->Observationdepth->Observation["id"].":".$item->Attribute["attributename"].":".$item->Observationdepth->Observation["location"].":".$item["attributevalue"].":".$item->Observationdepth->Observation["lat"].":".$item->Observationdepth->Observation["lng"].":".$year.":".$item->Observationdepth->Observation["remarks"].":".$item->Observationdepth->Observation["observationdate"].":".$item->Observationdepth["observationdepth"]."|";



    }
    echo $output;

    return sfView::NONE;
  }

  
  /// Set Search Filter

  public function executeSetsearchfilter(sfWebRequest $request)
  {
$speciesid=$request->getParameter('search');
$tag=$request->getParameter('tag');
$RID=$request->getParameter('RID');


if (isset($RID))
{
    $RID=" and r.id=".$RID;
}
  if (isset($speciesid))
  {
      if (isset($tag)&& $tag=="BIO")
      {$this->Datatype=$tag;

                  $this->searchfilterlocation=Doctrine_Query::create()
                  ->select('o.location')
                  ->from('observation o')
                  ->leftJoin('o.Observationdepth od')
                  ->leftJoin('od.Observationdepthdetail odd')
                  ->leftJoin('o.Research r')
                  ->where('odd.speciesid in ('.$speciesid.')'.$RID)
                  ->andWhere('odd.quantity>0')
                  ->execute();

                  $this->searchfiltercontributor=Doctrine_Query::create()
                  ->select('e.firstname')
                  ->from('Entity e')
                  ->leftJoin('e.Research r')
                  ->leftJoin('r.Observation o')
                  ->leftJoin('o.Observationdepth od')
                  ->leftJoin('od.Observationdepthdetail odd')
                  ->where('odd.speciesid in ('.$speciesid.')'.$RID)
                  ->andWhere('odd.quantity>0')
                  ->groupBy('e.firstname')
                  ->execute();

                  $this->searchfilterdepths=Doctrine_Query::create()
                  ->select('od.observationdepth')
                  ->from('observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('od.Observationdepthdetail odd')
                  ->leftJoin('o.Research r')
                  ->where('odd.speciesid in ('.$speciesid.')'.$RID)
                  ->andWhere('odd.quantity>0')
                  ->groupby('od.observationdepth')
                  ->execute();

                  $this->searchfiltertags=Doctrine_Query::create()
                  ->select('odd.tag')
                  ->from('observationdepthdetail odd')
                  ->leftJoin('odd.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('o.Research r')
                  ->where('odd.speciesid in ('.$speciesid.')'.$RID)
                  ->andWhere('odd.quantity>0')
                  ->groupby('odd.tag')
                  ->execute();

                    $this->searchfilterunits=Doctrine_Query::create()
                  ->select('u.unit')
                  ->from('unit u')
                  ->leftJoin('u.Observationdepthdetail odd')
                  ->leftJoin('odd.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('o.Research r')
                  ->where('odd.speciesid in ('.$speciesid.')'.$RID)
                  ->andWhere('odd.quantity>0')
                  ->groupby('u.unit')
                  ->execute();

                 $this->searchfilterdates=Doctrine_Query::create()
                  ->select('min(o.observationdate) as mindate,max(o.observationdate) as maxdate')
                  ->from('observation o')
                  ->leftJoin('o.Observationdepth od')
                  ->leftJoin('od.Observationdepthdetail odd')
                  ->leftJoin('o.Research r')
                  ->where('odd.speciesid in ('.$speciesid.')'.$RID)
                  ->andWhere('odd.quantity>0')
                  ->execute();

      }
      else if (isset($tag)&& $tag=="NONBIO")
      {$this->Datatype=$tag;

                  $this->searchfilterlocation=Doctrine_Query::create()
                  ->select('o.location')
                  ->from('observation o')
                  ->leftJoin('o.Observationdepth od')
                  ->leftJoin('od.Observationdepthattribute odd')
                  ->leftJoin('o.Research r')
                  ->where('odd.attributeid in ('.$speciesid.')' .$RID)
                  ->andWhere('odd.attributevalue>0')
                  ->execute();

                  $this->searchfiltercontributor=Doctrine_Query::create()
                  ->select('e.firstname')
                  ->from('Entity e')
                  ->leftJoin('e.Research r')
                  ->leftJoin('r.Observation o')
                  ->leftJoin('o.Observationdepth od')
                  ->leftJoin('od.Observationdepthattribute oda')
                  ->where('oda.attributeid in ('.$speciesid.')'.$RID)
                  ->andWhere('oda.attributevalue>0')
                  ->groupBy('e.firstname')
                  ->execute();

                  $this->searchfilterdepths=Doctrine_Query::create()
                  ->select('od.observationdepth')
                  ->from('observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('od.Observationdepthattribute odd')
                  ->leftJoin('o.Research r')
                  ->where('odd.attributeid in ('.$speciesid.')'.$RID)
                  ->andWhere('odd.attributevalue>0')
                  ->groupby('od.observationdepth')
                  ->execute();

                   $this->searchfilterunits=Doctrine_Query::create()
                  ->select('u.unit')
                  ->from('unit u')
                  ->leftJoin('u.Observationdepthattribute odd')
                  ->leftJoin('odd.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->leftJoin('o.Research r')
                  ->where('odd.attributeid in ('.$speciesid.')'.$RID)
                  ->andWhere('odd.attributevalue>0')                          
                  ->groupby('u.unit')
                  ->execute();

                   $this->searchfilterdates=Doctrine_Query::create()
                  ->select('min(o.observationdate) as mindate,max(o.observationdate) as maxdate')
                  ->from('observation o')
                  ->leftJoin('o.Observationdepth od')
                  ->leftJoin('od.Observationdepthattribute odd')
                  ->leftJoin('o.Research r')
                  ->where('odd.attributeid in ('.$speciesid.')'.$RID)
                  ->andWhere('odd.attributevalue>0')
                  ->execute();
      }
  }
  

  }
}
